package eu.zuno.jsf;

import java.util.Date;

import javax.annotation.Resource;
import javax.faces.bean.SessionScoped;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;


@javax.faces.bean.ManagedBean
@SessionScoped
public class Bean {
	private String message;
	
	@Resource(mappedName = "jms/Reqource")
	private ConnectionFactory connectionFactory;

	@Resource(mappedName = "jms/mx.zuno.log")
	private Queue queue;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public void send(){
		
		Connection connection;
		try {
			connection = connectionFactory.createConnection();

			Session session = connection.createSession(false,
					Session.AUTO_ACKNOWLEDGE);
			MessageProducer producer = session.createProducer(queue);
			TextMessage message = session.createTextMessage();
			message.setText("This is a text message sent at " + new Date());
			producer.send(message);
			connection.close();

		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			System.out.println("message sent");
		}
	}
}
